﻿using Castle.Components.DictionaryAdapter;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using PanhuaWin.NCModel;
using PanhuaWin.系统管理;
using PanhuaWin.通用;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace PanhuaWin.仓库.生产领用
{
    public partial class Form原料卷领用 : UIPage
    {

        string 工厂guid1;
        string 部门编号;
        string FormGuid;
        int 开始行;
        int 结束行;
        string 部门guid;
        string FormWorkType = "酸洗";
        public Form原料卷领用()
        {
            InitializeComponent();
        }

        private void Form原料卷领用_Resize(object sender, EventArgs e)
        {

        }

        private void uiCheckBox选中行打钩_CheckedChanged(object sender, EventArgs e)
        {
            if (开始行 <= 结束行)
            {

            }
            else
            {
                int p = 开始行;
                开始行 = 结束行;
                结束行 = p;
            }
            if (开始行 >= 0 && 结束行 >= 0)
            {
                for (int i = 开始行; i <= 结束行; i++)
                {
                    this.uiDataGridView清单.Rows[i].Cells["D选中"].Value = this.uiCheckBox选中行打钩.Checked;
                }
            }
        }

        private void Form原料卷领用_Load(object sender, EventArgs e)
        {
            this.uiDatePicker1.Value = DateTime.Today.AddMonths(-1);
            this.uiDatePicker2.Value = DateTime.Today;
            pub.ListWorkLine(pub.全局工厂编号, "酸洗", this.uiComboBox产线);
            this.uiTextBox工厂名称.Text = pub.全局工厂名称;
            pub.ListSteelAll(this.uiComboBox材质1);
            FormGuid = Guid.NewGuid().ToString();

        }



        private void uiComboBox产线_SelectedIndexChanged(object sender, EventArgs e)
        {
            string workLineCode;
            if (this.uiComboBox产线.SelectedValue != null)
            {
                workLineCode = this.uiComboBox产线.SelectedValue.ToString();
                pub.ListWorkTypeAll(pub.全局工厂编号, workLineCode, this.uiComboBox去向);
            }
            this.uiDataGridView清单.DataSource = null;
            this.uiDataGridView清单.Refresh();


        }

        private void uiCheckBox1_CheckedChanged(object sender, EventArgs e)
        {

        }


        private void uiButton查询_Click(object sender, EventArgs e)
        {
            this.uiDataGridView清单.DataSource = null;
            this.uiDataGridView清单.Refresh();
            ListData();

        }

        private void ListData()
        {
            System.Data.DataTable tb1 = GetList();
            if (tb1.Rows.Count > 0)
            {
                this.uiDataGridView清单.AutoGenerateColumns = false;
                this.uiDataGridView清单.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView清单.DataSource = tb1;
                this.uiDataGridView清单.Refresh();
            }
            else
            {
                this.uiDataGridView清单.DataSource = null;
                this.uiDataGridView清单.Refresh();
            }
        }

        private DataTable GetList()
        {
            string sql = @"SELECT  row_number() over(order by  CreateDate) 序号, WorkShopGuid, WorkShopCode, DepartGuid, DepartCode 部门编号, WorkLineCode, WorkLineName 产线名称, WorkDate, RowNumber,
                           Originalsize 热轧规格, Address 产地, VolumeCode , VolumeCodeNew 热轧批号, Weight 重量, OutputSize 轧制规格, ProductSize 成品规格,NextWorkType 去向, Material 材质, StorageLocation 库位, MAC,
                          CreateDate, remark 备注,Approver 发料人 ,ApproverDate 发料时间,Customer 客户 FROM Bus_PlanPickling  where 1=1   ";
            sql += " and CreateDate>='" + this.uiDatePicker1.Value.ToString("yyyy-MM-dd") + "'";
            sql += " and CreateDate<'" + this.uiDatePicker2.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            if (!this.uiCheckBox包含已发出.Checked)
            {
                //sql += " and VolumeCodeNew not in (select VolumeCodeNew from Bus_StockOutVolume) ";
                sql += " and  (Approver is null or Approver='')";
            }
            string WorkShopCode = pub.全局工厂编号;
            sql += " and WorkShopCode='" + WorkShopCode + "'";

            if (this.uiComboBox材质1.SelectedText != "全部" && this.uiComboBox材质1.SelectedText != "")
            {
                string Material = this.uiComboBox材质1.SelectedText.Trim();
                sql += " and Material='" + Material + "'";
            }
            if (this.uiCheckBox1.Checked)
            {
                sql += " and WorkLineName='" + this.uiComboBox产线.SelectedText + "'";
            }

            if (this.uiComboBox去向.SelectedText != "全部" && this.uiComboBox去向.SelectedText != "")
            {
                string NextWorkType = this.uiComboBox去向.SelectedText.ToString().Trim();
                sql += " and NextWorkType='" + NextWorkType + "'";
            }
            if (this.uiTextBox热轧规格.Text != "")
            {
                string Originalsize = this.uiTextBox热轧规格.Text.Trim().Replace("--", "");
                sql += " and Originalsize='" + Originalsize + "'";
            }
            if (this.uiTextBox产地1.Text != "")
            {
                string Address = this.uiTextBox产地1.Text.Trim().Replace("--", "");
                sql += " and Address='" + Address + "'";
            }
            if (this.uiTextBox成品规格.Text != "")
            {
                string ProductSize = this.uiTextBox成品规格.Text.Trim().Replace("--", "");
                sql += " and ProductSize='" + ProductSize + "'";
            }
            if (this.uiTextBox轧制规格.Text != "")
            {
                string OutputSize = this.uiTextBox轧制规格.Text.Trim().Replace("--", "");
                sql += " and OutputSize='" + OutputSize + "'";
            }


            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            return tb;
        }
        private DataTable GetListShort()
        {
            string sql = @"SELECT  row_number() over(order by  CreateDate) 序号, 
               Originalsize 热轧规格, Address 产地,  VolumeCodeNew 热轧批号, Weight 重量, OutputSize 轧制规格, ProductSize 成品规格,NextWorkType 去向, Material 材质, StorageLocation 库位, 
                remark 备注,Approver 审批人 ,ApproverDate 审批时间,Customer 客户,IsSendToNC,SendToNCResult FROM Bus_PlanPickling  where 1=1   ";
            sql += " and CreateDate>='" + this.uiDatePicker1.Value.ToString("yyyy-MM-dd") + "'";
            sql += " and CreateDate<'" + this.uiDatePicker2.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            if (!this.uiCheckBox包含已发出.Checked)
            {

                sql += " and  (Approver is null or Approver='')";
            }
            string WorkShopCode = pub.全局工厂编号;
            sql += " and WorkShopCode='" + WorkShopCode + "'";

            if (this.uiComboBox材质1.SelectedText != "全部" && this.uiComboBox材质1.SelectedText != "")
            {
                string Material = this.uiComboBox材质1.SelectedText.Trim();
                sql += " and Material='" + Material + "'";
            }
            if (this.uiCheckBox1.Checked)
            {
                sql += " and WorkLineName='" + this.uiComboBox产线.SelectedText + "'";
            }

            if (this.uiComboBox去向.SelectedText != "全部" && this.uiComboBox去向.SelectedText != "")
            {
                string NextWorkType = this.uiComboBox去向.SelectedText.ToString().Trim();
                sql += " and NextWorkType='" + NextWorkType + "'";
            }
            if (this.uiTextBox热轧规格.Text != "")
            {
                string Originalsize = this.uiTextBox热轧规格.Text.Trim().Replace("--", "");
                sql += " and Originalsize='" + Originalsize + "'";
            }
            if (this.uiTextBox产地1.Text != "")
            {
                string Address = this.uiTextBox产地1.Text.Trim().Replace("--", "");
                sql += " and Address='" + Address + "'";
            }
            if (this.uiTextBox成品规格.Text != "")
            {
                string ProductSize = this.uiTextBox成品规格.Text.Trim().Replace("--", "");
                sql += " and ProductSize='" + ProductSize + "'";
            }
            if (this.uiTextBox轧制规格.Text != "")
            {
                string OutputSize = this.uiTextBox轧制规格.Text.Trim().Replace("--", "");
                sql += " and OutputSize='" + OutputSize + "'";
            }


            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            return tb;
        }

        private void uiButton导出_Click(object sender, EventArgs e)
        {
            pub.ToExcel(GetList());
        }

        private void uiDataGridView清单_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            开始行 = e.RowIndex;
        }

        private void uiDataGridView清单_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            结束行 = e.RowIndex;
        }

        private void uiButton选中行发出_Click(object sender, EventArgs e)
        {
            开始行 = -1;
            结束行 = -1;
            bool tags = false;
            string VolumeCodeNew;
            string StorageLocation;
            string StockOutType = "原料出库";
            string sql;
            string WorkShopCode = pub.全局工厂编号;
            if (this.uiDataGridView清单.Rows.Count > 0)
            {
                ///先验证是否出库了 
                for (int j = 0; j < this.uiDataGridView清单.Rows.Count; j++)
                {
                    DataGridViewRow urow = this.uiDataGridView清单.Rows[j];
                    if (urow.Cells["D选中"].Value != null)
                    {
                        if ((bool)urow.Cells["D选中"].Value)
                        {
                            VolumeCodeNew = this.uiDataGridView清单.Rows[j].Cells["D热轧批号"].Value.ToString();  //
                            StorageLocation = this.uiDataGridView清单.Rows[j].Cells["D库位"].Value.ToString();  //
                            if (!pub.IsVolumeInStorage(WorkShopCode, VolumeCodeNew, "YLK"))
                            {
                                MessageBox.Show("要发料的卷【" + VolumeCodeNew + "】不在仓库!", "提示", MessageBoxButtons.OK);
                                return;
                            }
                        }
                    }
                }


                string docCode = pub.GetDocCode(StockOutType, WorkShopCode);
                Dic_Depart dic_Depart = pub.GetDepartByWorkLine(this.uiComboBox产线.SelectedText);
                if (dic_Depart != null)
                {
                    部门编号 = dic_Depart.DepartName;
                    部门guid = dic_Depart.guid.ToString();

                }
                string guid = Guid.NewGuid().ToString();
                int k = 1;
                for (int j = 0; j < this.uiDataGridView清单.Rows.Count; j++)
                {
                    DataGridViewRow urow = this.uiDataGridView清单.Rows[j];
                    if (urow.Cells["D选中"].Value != null)
                    {
                        if ((bool)urow.Cells["D选中"].Value)
                        {
                            //列出值
                            //string sqlquery = @"select Bus_PlanPickling.guid,WarehouseCode,Bus_PlanPickling.StorageLocation, Bus_StockInVolume.VolumeCode,";
                            //sqlquery += "Bus_StockInVolume.VolumeCodeNew,InventoryCode,Size,Thick, Width, ";
                            //sqlquery += "      Bus_StockInVolume.Material,Virtual,Bus_StockInVolume.Weight,OutputSize,ProductSize,NextWorkType,";
                            //sqlquery += "      Bus_PlanPickling.Remark,CID,Bus_StockInVolume.Address";
                            //sqlquery += "        from  Bus_StockInVolume join Bus_PlanPickling";
                            //sqlquery += "        on Bus_StockInVolume.VolumeCodeNew = Bus_PlanPickling.VolumeCodeNew";
                            //sqlquery += "        where Bus_PlanPickling.VolumeCodeNew = '" +
                            //                    this.uiDataGridView清单.Rows[j].Cells["D热轧批号"].Value.ToString()
                            //                    + "' and Bus_PlanPickling.workShopCode = '" + WorkShopCode + "'";

                            //DataTable dt = pub.GetDataTable(pub.ConnectionString, sqlquery);
                            //if (dt != null && dt.Rows.Count > 0)
                            //{
                            //    string 公司名称 = dic_Depart.DepartName;
                            //    string 地区 = "华中";
                            //    string 制单日期 = urow.Cells["D发料时间"].Value.ToString();
                            //    string 仓库编号 = dt.Rows[0]["WarehouseCode"].ToString();
                            //    string cWhsmanagerCode = pub.sys_user.UserName;
                            //    string 部门名称 = 部门编号;
                            //    string 业务员编码 = pub.sys_user.UserName;
                            //    string 备注 = "";
                            //    string 明细id = dt.Rows[0]["guid"].ToString();
                            //    string 存货编码 = dt.Rows[0]["InventoryCode"].ToString();
                            //    string 批次号 = dt.Rows[0]["VolumeCodeNew"].ToString();
                            //    string 入库数量 = dt.Rows[0]["Weight"].ToString();
                            //    string 入库日期 = DateTime.Now.ToString("yyyy-MM-dd");
                            //    string 库位 = dt.Rows[0]["StorageLocation"].ToString();
                            //    //发送NC
                            //    NCModel.NCResponseModel nCResponse = send原来领用ToNC("2001", 地区, 制单日期, 仓库编号, cWhsmanagerCode, 部门名称, 业务员编码
                            //                                                        , 备注, 明细id, 存货编码, 批次号, 入库数量, 入库日期, 库位);
                            //    //调用成功，改写NC状态
                            //    if (nCResponse.state == 0)
                            //    {
                            //        //更新出库表
                            //        string sqlUpdate = @"update Bus_PlanPickling set IsSendToNC = 'Y',SendToNCResult = '" + nCResponse.result + "' ,SendToNCDate = '"
                            //                            + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where guid = '" + 明细id + "'";
                            //        int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "原料卷入库发送到NC");
                            //        if (r < 1)
                            //        {
                            //            MessageBox.Show("更新成功发送到NC失败");
                            //        }
                            //    }
                            //    else
                            //    {
                            //        string sqlUpdate = @"update Bus_PlanPickling set IsSendToNC = 'N',SendToNCResult = '" + nCResponse.msg + "' ,SendToNCDate = '"
                            //                            + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where guid = '" + 明细id + "'";
                            //        int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "原料卷入库发送到NC");
                            //        if (r < 1)
                            //        {
                            //            MessageBox.Show("更新失败发送到NC失败");
                            //        }
                            //    }
                            //}
                            //else
                            //{
                            //    return;
                            //}

                            ///先验证是否重复了
                            if (!tags)  ///没有表头时
                            {

                                //InsertBus_StockOutV
                                sql = " insert into Bus_StockOutV(guid, StockOutType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
                                sql += " values('" + guid + "','" + StockOutType + "','" + WorkShopCode + "','" + docCode + "', '" + 部门编号 + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'N')";
                                if (pub.ExecuteSql(pub.ConnectionString, sql, "酸洗计划原料审核出库塞入Bus_StockOutV") > 0)
                                {
                                    tags = true;
                                }
                            }
                            if (tags)  //塞入表身
                            {
                                VolumeCodeNew = this.uiDataGridView清单.Rows[j].Cells["D热轧批号"].Value.ToString();  //
                                sql = @" insert into Bus_StockOutVolume(guid,Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, 
                                        VolumeCode, VolumeCodeNew,InventoryCode, Size, Thick, Width, Material,Virtual, Weight, OutputSize,ProductSize,NextWorkType,Remark,CID ,Address )";
                                sql += " select  '" + guid + "',newid(),'" + docCode + "',  '" + k + "','" + WorkShopCode + "',	WarehouseCode,Bus_PlanPickling.StorageLocation, Bus_StockInVolume.VolumeCode,";
                                sql += " Bus_StockInVolume.VolumeCodeNew,InventoryCode,Size,Thick, Width, ";
                                sql += " Bus_StockInVolume.Material,Virtual,Bus_StockInVolume.Weight,OutputSize,ProductSize,NextWorkType,Bus_PlanPickling.Remark,CID,Bus_StockInVolume.Address ";
                                sql += "  from  Bus_StockInVolume join Bus_PlanPickling ";
                                sql += " on Bus_StockInVolume.VolumeCodeNew = Bus_PlanPickling.VolumeCodeNew  where Bus_PlanPickling.VolumeCodeNew='" + VolumeCodeNew + "' and Bus_PlanPickling.workShopCode='" + WorkShopCode + "'";
                                if (pub.ExecuteSql(pub.ConnectionString, sql, "酸洗计划原料审核出库塞入Bus_StockOutVolume") > 0)
                                {
                                    sql = " update Bus_PlanPickling set ApproverID='" + pub.sys_user.User_Id + "' ,Approver='" + pub.sys_user.UserTrueName + "' ,ApproverDate =getdate()  ";
                                    sql += " where VolumeCodeNew='" + VolumeCodeNew + "' and workShopCode='" + WorkShopCode + "'";
                                    pub.ExecuteSql(pub.ConnectionString, sql, "更新酸洗计划Bus_PlanPickling审批状态");

                                    k++;
                                }
                                else
                                {
                                    MessageBox.Show("批量确认不成功!", "提示", MessageBoxButtons.OK);
                                    return;
                                }
                            }
                            else
                            {
                                MessageBox.Show("批量确认不成功!", "提示", MessageBoxButtons.OK);
                                return;
                            }
                        }
                    }
                }
                //抛NC
                // NCModel.NCResponseModel nCResponse = send原来领用ToNC(guid);

                this.uiDataGridView清单.DataSource = null;
                this.uiDataGridView清单.Refresh();
                ListData();
            }
        }


        //private NCResponseModel send原来领用ToNC(string guid)
        //{
        //    NCModel.NCResponseModel nCResponse = new NCResponseModel();
        //    //查询领用出库单
        //    string sqlHeader = @"SELECT  guid, StockOutType, WorkShopCode, DocCode, DepartCode, CreateID, Creator, CreateDate, ModifyID, Modifier, 
        //                   ModifyDate, ApproverID, Approver, ApproverDate, IsSendToNC, SendToNCDate
        //                    FROM      Bus_StockOutV where guid = '" + guid + "'";
        //    DataTable dtHeader = pub.GetDataTable(pub.ConnectionString, sqlHeader);
        //    if (dtHeader != null && dtHeader.Rows.Count > 0)
        //    {

        //        //头
        //        string region = "华中";//地区
        //        string createdate = dtHeader.Rows[0]["CreateDate"].ToString();
        //        string WorkShopCode = dtHeader.Rows[0]["WorkShopCode"].ToString();//公司编号
        //        string corpName = "";
        //        string cWhsmanagerCode = dtHeader.Rows[0]["CreateID"].ToString();
        //        string cbizCode = dtHeader.Rows[0]["CreateID"].ToString(); //业务员编码 
        //        string wareHouseCode = "FYLK";
        //        string cWarehouseName = "";


        //        //查询公司名称 公司编号
        //        string sqlworkshop = @"SELECT  [WorkShopName]  FROM [Dic_WorkShop] where WorkShopCode = '" + WorkShopCode + "'";
        //        DataTable dtWorkShop = pub.GetDataTable(pub.ConnectionString, sqlworkshop);
        //        if (dtWorkShop != null && dtWorkShop.Rows.Count > 0)
        //        {
        //            corpName = dtWorkShop.Rows[0]["WorkShopName"].ToString();
        //        }

        //        //查仓库名称
        //        string sqlwareName = "select WarehouseName from Dic_Warehouse where WarehouseCode = '" + wareHouseCode + "'";

        //        DataTable dtWareName = pub.GetDataTable(pub.ConnectionString, sqlwareName);
        //        if (dtWareName != null && dtWareName.Rows.Count > 0)
        //        {
        //            cWarehouseName = dtWareName.Rows[0]["WarehouseName"].ToString();
        //        }

        //        //NC对象
        //        MaterialOutBillModel materialOutBillModel = new MaterialOutBillModel();
        //        materialOutBillModel.region = region;
        //        materialOutBillModel.createDate = createdate;
        //        materialOutBillModel.corpName = corpName;
        //        materialOutBillModel.cWhsmanagerCode = cWhsmanagerCode;
        //        materialOutBillModel.cbizCode = cbizCode;
        //        materialOutBillModel.cWarehouseName = cWarehouseName;
        //        materialOutBillModel.remarks = "";

        //        //查表体
        //        string sqlBody = @"SELECT   guid, Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, VolumeCode, 
        //                       VolumeCodeNew, InventoryCode, InventoryName, Size, Thick, Width, Material, Address, Weight, CID, Virtual, OutputSize, 
        //                       ProductSize, NextWorkType, Remark,IsSendToNC,SendToNCResult
        //                        FROM Bus_StockOutVolume where guid = '" + guid + "'";
        //        DataTable dtBody = pub.GetDataTable(pub.ConnectionString, sqlBody);
        //        if (dtBody != null && dtBody.Rows.Count > 0)
        //        {
        //            List<MaterialOutBillItemModel> items = new List<MaterialOutBillItemModel>();
        //            for (int i = 0; i < dtBody.Rows.Count; i++)
        //            {
        //                //体
        //                string rowNo = dtBody.Rows[i]["Bguid"].ToString(); //明细ID
        //                string cinvbascode = dtBody.Rows[i]["InventoryCode"].ToString(); //存货编码
        //                string vbatchcode = dtBody.Rows[i]["VolumeCodeNew"].ToString(); //批次号
        //                string nnum = dtBody.Rows[i]["Weight"].ToString(); //入库数量
        //                string dbilldate = dtHeader.Rows[0]["CreateDate"].ToString();//入库日期
        //                string vuserdef9 = dtBody.Rows[i]["StorageLocation"].ToString();//库位号
        //                MaterialOutBillItemModel materialOutBillItemModel = new MaterialOutBillItemModel();
        //                materialOutBillItemModel.rowNo = rowNo;
        //                materialOutBillItemModel.cinvbascode = cinvbascode;
        //                materialOutBillItemModel.vbatchcode = vbatchcode;
        //                materialOutBillItemModel.nnum = nnum;
        //                materialOutBillItemModel.dbilldate = dbilldate;
        //                materialOutBillItemModel.vuserdef9 = vuserdef9;
        //                materialOutBillItemModel.vfree1 = "";
        //                materialOutBillItemModel.vfree2 = "";
        //                materialOutBillItemModel.vfree3 = "";
        //                materialOutBillItemModel.vfree4 = "";
        //                materialOutBillItemModel.vfree5 = "";
        //                materialOutBillItemModel.pk_defdoc11 = "";
        //                materialOutBillItemModel.pk_defdoc12 = "";
        //                materialOutBillItemModel.vnotebody = "";
        //                materialOutBillItemModel.vuserdef8 = "";
        //                materialOutBillItemModel.vuserdef10 = "";
        //                materialOutBillItemModel.pk_defdoc13 = "";

        //                items.Add(materialOutBillItemModel);
        //            }
        //        }
        //        string jsonStr = JsonConvert.SerializeObject(materialOutBillModel);
        //        WMS2NCTest.ExternalSystemToNcItfPortTypeClient client = new WMS2NCTest.ExternalSystemToNcItfPortTypeClient();
        //        string responseStr = client.materialOutBill(jsonStr);
        //        nCResponse = JsonConvert.DeserializeObject<NCModel.NCResponseModel>(responseStr);
        //        //调用成功，改写NC状态
        //        if (nCResponse.state == 0)
        //        {
        //            //更新出库表
        //            string sqlUpdate = @"update Bus_StockOutVolume set IsSendToNC = 'Y',SendToNCResult = '" + nCResponse.result + "' ,SendToNCDate = '"
        //                                + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where guid = '" + guid + "'";
        //            int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "原料卷入库发送到NC");
        //            if (r < 1)
        //            {
        //                MessageBox.Show("更新成功发送到NC失败");
        //            }
        //        }
        //        else
        //        {
        //            string sqlUpdate = @"update Bus_StockOutVolume set IsSendToNC = 'N',SendToNCResult = '" + nCResponse.msg + "' ,SendToNCDate = '"
        //                                + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where guid = '" + guid + "'";
        //            int r = pub.ExecuteSql(pub.ConnectionString, sqlUpdate, "原料卷入库发送到NC");
        //            if (r < 1)
        //            {
        //                MessageBox.Show("更新失败发送到NC失败");
        //            }
        //        }
        //    }
        //    return nCResponse;
        //}

        //private NCResponseModel send原来领用ToNC(string 公司编号, string 地区, string 制单日期, string 仓库编号, string 库管员编码, string 产线, string 业务员编码
        //                                        , string 备注
        //                                        , string 明细id, string 存货编码, string 批次号, string 入库数量, string 入库日期, string 库位)
        //{
        //    NCModel.NCResponseModel nCResponse = new NCResponseModel();

        //    //头
        //    string corpName = "";
        //    string cWarehouseName = "";
        //    string 公司名称 = "";


        //    //查询公司名称 公司编号
        //    string sqlworkshop = @"SELECT  [WorkShopName]  FROM [Dic_WorkShop] where WorkShopCode = '" + 公司编号 + "'";
        //    DataTable dtWorkShop = pub.GetDataTable(pub.ConnectionString, sqlworkshop);
        //    if (dtWorkShop != null && dtWorkShop.Rows.Count > 0)
        //    {
        //        公司名称 = dtWorkShop.Rows[0]["WorkShopName"].ToString();
        //    }

        //    //查仓库名称
        //    string sqlwareName = "select WarehouseName from Dic_Warehouse where WarehouseCode = '" + 仓库编号 + "'";

        //    DataTable dtWareName = pub.GetDataTable(pub.ConnectionString, sqlwareName);
        //    if (dtWareName != null && dtWareName.Rows.Count > 0)
        //    {
        //        cWarehouseName = dtWareName.Rows[0]["WarehouseName"].ToString();
        //    }

        //    //NC对象
        //    MaterialOutBillModel materialOutBillModel = new MaterialOutBillModel();
        //    materialOutBillModel.region = 地区;
        //    materialOutBillModel.createDate = 制单日期;
        //    materialOutBillModel.corpName = 公司名称;
        //    materialOutBillModel.cWhsmanagerCode = 库管员编码;
        //    materialOutBillModel.cbizCode = 业务员编码;
        //    materialOutBillModel.cWarehouseName = cWarehouseName;
        //    materialOutBillModel.deptName = 产线;
        //    materialOutBillModel.remarks = 备注;

        //    List<MaterialOutBillItemModel> items = new List<MaterialOutBillItemModel>();

        //    //体
        //    string rowNo = 明细id; //明细ID
        //    string cinvbascode = 存货编码; //存货编码
        //    string vbatchcode = 批次号; //批次号
        //    string nnum = 入库数量; //入库数量
        //    string dbilldate = 入库日期;//入库日期
        //    string vuserdef9 = 库位;//库位号
        //    MaterialOutBillItemModel materialOutBillItemModel = new MaterialOutBillItemModel();
        //    materialOutBillItemModel.rowNo = rowNo;
        //    materialOutBillItemModel.cinvbascode = cinvbascode;
        //    materialOutBillItemModel.vbatchcode = vbatchcode;
        //    materialOutBillItemModel.nnum = nnum;
        //    materialOutBillItemModel.dbilldate = dbilldate;
        //    materialOutBillItemModel.vuserdef9 = vuserdef9;
        //    materialOutBillItemModel.vfree1 = "";
        //    materialOutBillItemModel.vfree2 = "";
        //    materialOutBillItemModel.vfree3 = "";
        //    materialOutBillItemModel.vfree4 = "";
        //    materialOutBillItemModel.vfree5 = "";
        //    materialOutBillItemModel.pk_defdoc11 = "";
        //    materialOutBillItemModel.pk_defdoc12 = "";
        //    materialOutBillItemModel.vnotebody = "";
        //    materialOutBillItemModel.vuserdef8 = "";
        //    materialOutBillItemModel.vuserdef10 = "";
        //    materialOutBillItemModel.pk_defdoc13 = "";
        //    items.Add(materialOutBillItemModel);

        //    string jsonStr = JsonConvert.SerializeObject(materialOutBillModel);
        //    WMS2NCTest.ExternalSystemToNcItfPortTypeClient client = new WMS2NCTest.ExternalSystemToNcItfPortTypeClient();
        //    string responseStr = client.materialOutBill(jsonStr);
        //    nCResponse = JsonConvert.DeserializeObject<NCModel.NCResponseModel>(responseStr);
        //    return nCResponse;
        //}


        private void uiDatePicker1_ValueChanged(object sender, DateTime value)
        {
            this.uiDataGridView清单.DataSource = null;
            this.uiDataGridView清单.Refresh();
        }

        private void uiDatePicker2_ValueChanged(object sender, DateTime value)
        {
            this.uiDataGridView清单.DataSource = null;
            this.uiDataGridView清单.Refresh();
        }

        private void uiComboBox材质1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.uiDataGridView清单.DataSource = null;
            this.uiDataGridView清单.Refresh();
        }



        private void uiComboBox去向_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.uiDataGridView清单.DataSource = null;
            this.uiDataGridView清单.Refresh();
        }

        private void uiDataGridView清单_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            开始行 = -1;
            结束行 = -1;
            bool tags = false;
            string VolumeCodeNew;
            string StorageLocation;
            string StockOutType = "原料出库";
            string sql;
            string WorkShopCode = pub.全局工厂编号;
            if (this.uiDataGridView清单.Columns[e.ColumnIndex].Name == "D发料确认")
            {
                #region  D发料确认
                int j = e.RowIndex;
                if (j >= 0)
                {
                    if (this.uiDataGridView清单.Rows.Count > 0)
                    {

                        VolumeCodeNew = this.uiDataGridView清单.Rows[j].Cells["D热轧批号"].Value.ToString();  //
                        StorageLocation = this.uiDataGridView清单.Rows[j].Cells["D库位"].Value.ToString();  //
                        if (!pub.IsVolumeInStorage(WorkShopCode, VolumeCodeNew, "YLK"))
                        {
                            MessageBox.Show("要发料的卷【" + VolumeCodeNew + "】不在库!", "提示", MessageBoxButtons.OK);
                            return;
                        }

                    }
                }

                ///

                string docCode = pub.GetDocCode(StockOutType, WorkShopCode);
                //if (this.uiTextBox部门1.Text == "")
                //{
                //    MessageBox.Show("部门未选择!", "提示", MessageBoxButtons.OK);
                //    return;
                //}
                string departCode = this.uiDataGridView清单.Rows[j].Cells["D部门编号"].Value.ToString(); ;
                string guid = Guid.NewGuid().ToString();
                int k = 1;

                DataGridViewRow urow = this.uiDataGridView清单.Rows[j];
                ///先验证是否重复了
                if (!tags)  ///没有表头时
                {
                    //InsertBus_StockOutV
                    sql = " insert into Bus_StockOutV(guid, StockOutType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
                    sql += " values('" + guid + "','" + StockOutType + "','" + WorkShopCode + "','" + docCode + "', '" + departCode + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'N')";
                    if (pub.ExecuteSql(pub.ConnectionString, sql, "酸洗计划原料审核出库塞入Bus_StockOutV") > 0)
                    {
                        tags = true;
                    }
                }
                if (tags)  //塞入表身
                {
                    VolumeCodeNew = this.uiDataGridView清单.Rows[j].Cells["D热轧批号"].Value.ToString();  //
                    sql = @" insert into Bus_StockOutVolume(guid,Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, 
                VolumeCode, VolumeCodeNew,InventoryCode, Size, Thick, Width, Material,Virtual, Weight, OutputSize,ProductSize,NextWorkType,Remark,CID ,Address )";
                    sql += " select  '" + guid + "',newid(),'" + docCode + "',  '" + k + "','" + WorkShopCode + "', WarehouseCode,StorageLocation,";
                    sql += " VolumeCode,VolumeCodeNew,InventoryCode,Size,Thick, Width, Material,Virtual,Weight,OutputSize,ProductSize,NextWorkType,Remark,CID,Address ";
                    sql += " from  Bus_StockInVolume where VolumeCodeNew='" + VolumeCodeNew + "' and workShopCode='" + WorkShopCode + "'";
                    if (pub.ExecuteSql(pub.ConnectionString, sql, "酸洗计划原料审核出库塞入Bus_StockOutVolume") > 0)
                    {
                        sql = " update Bus_PlanPickling set ApproverID='" + pub.sys_user.User_Id + "' ,Approver='" + pub.sys_user.UserTrueName + "' ,ApproverDate =getdate()  ";
                        sql += " where VolumeCodeNew='" + VolumeCodeNew + "' and workShopCode='" + WorkShopCode + "'";
                        pub.ExecuteSql(pub.ConnectionString, sql, "更新酸洗计划Bus_PlanPickling审批状态");
                        k++;
                    }
                    else
                    {
                        MessageBox.Show("确认不成功!", "提示", MessageBoxButtons.OK);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("确认不成功!", "提示", MessageBoxButtons.OK);
                    return;
                }
                ////重新刷新
                this.uiDataGridView清单.DataSource = null;
                this.uiDataGridView清单.Refresh();
                ListData();
                #endregion
            }

            else if (this.uiDataGridView清单.Columns[e.ColumnIndex].Name == "D计划删除")
            {
                #region  D计划删除
                int j = e.RowIndex;
                if (j >= 0)
                {
                    if (this.uiDataGridView清单.Rows.Count > 0)
                    {

                        VolumeCodeNew = this.uiDataGridView清单.Rows[j].Cells["D热轧批号"].Value.ToString();  //
                        StorageLocation = this.uiDataGridView清单.Rows[j].Cells["D库位"].Value.ToString();  //
                        if (pub.IsPurchaseVolumeBeUsed(WorkShopCode, VolumeCodeNew, StorageLocation))
                        {
                            MessageBox.Show("要发料的卷【" + VolumeCodeNew + "】被使用了!", "提示", MessageBoxButtons.OK);
                            return;
                        }

                    }
                }

                ////重新刷新
                this.uiDataGridView清单.DataSource = null;
                this.uiDataGridView清单.Refresh();
                ListData();
                #endregion
            }


        }

        private void uiLabel28_Click(object sender, EventArgs e)
        {

        }

        private void uiTextBox部门1_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiTextBox部门名称1_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiButton全选_Click(object sender, EventArgs e)
        {
            if (this.uiDataGridView清单.Rows.Count > 0)
            {
                ///先验证是否出库了
                for (int j = 0; j < this.uiDataGridView清单.Rows.Count; j++)
                {
                    DataGridViewRow urow = this.uiDataGridView清单.Rows[j];
                    //if (urow.Cells["D选中"].Value != null)
                    //{
                    //}
                    urow.Cells["D选中"].Value = true;
                }
            }
        }
    }
}
